package com.sinosoft.app.notice.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.app.notice.dao.RuleNoticeDao;
import com.sinosoft.app.notice.entity.RuleNotice;

   /**
    * @ClassName RuleNoticeDaoJdbc实现类
    * @date 2017-2-13 
    * @author shangxp
    */  
@Repository
public class RuleNoticeDaoJdbc implements RuleNoticeDao {
	private static Logger log = Logger.getLogger(RuleNoticeDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertRuleNotice(RuleNotice ruleNotice) {
		String sqlStr = "insert into rule_notice ( "
							+" id ,"	
							+" title ,"	
							+" sketch ,"	
							+" maintxt ,"	
							+" releasetime ,"	
							+" category ,"	
							+" module ,"	
							+" releaser ,"	
							+"is_del) values( "
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" 0 )";
		log.debug(sqlStr+"\n"+ruleNotice.toString());
		try{
			Object[] args = {
					ruleNotice.getId(),
					ruleNotice.getTitle(),
					ruleNotice.getSketch(),
					ruleNotice.getMaintxt(),
					ruleNotice.getReleasetime(),
					ruleNotice.getCategory(),
					ruleNotice.getModule(),
					ruleNotice.getReleaser()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+ruleNotice.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateRuleNotice(RuleNotice ruleNotice) {
		String sqlStr = "update rule_notice set "
									+" id = ? , "
									+" title = ? , "
									+" sketch = ? , "
									+" maintxt = ? , "
									+" releasetime = ? , "
									+" category = ? , "
									+" module = ? , "
									+" releaser = ?  "
									+" where id = ?";
		log.debug(sqlStr+"\n"+ruleNotice.toString());
		try{
			Object[] args = {
					ruleNotice.getId(),
					ruleNotice.getTitle(),
					ruleNotice.getSketch(),
					ruleNotice.getMaintxt(),
					ruleNotice.getReleasetime(),
					ruleNotice.getCategory(),
					ruleNotice.getModule(),
					ruleNotice.getReleaser(),
					ruleNotice.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+ruleNotice.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteRuleNotice(int id) {
		String sqlStr = "update rule_notice set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public RuleNotice selectRuleNotice(int id) {
		String sqlStr = "select * from rule_notice where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public RuleNotice selectRuleNotice(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from rule_notice where is_del =0 " );
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+"=? ");
				args[i] = queryValues.get(i);
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountRuleNotice() {
		String sqlStr = "select count(1) from rule_notice where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<RuleNotice> selectLimitRuleNotice(int start, int size) {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" title ,";
					sqlStr+=" sketch ,";
					sqlStr+=" maintxt ,";
					sqlStr+=" releasetime ,";
					sqlStr+=" category ,";
					sqlStr+=" module ,";
					sqlStr+=" releaser ";
			sqlStr+=" from rule_notice where is_del =0  order by id desc limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				size
		};
			return jdbcTemplate.query(sqlStr, args, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<RuleNotice> selectAllRuleNotice() {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" title ,";
					sqlStr+=" sketch ,";
					sqlStr+=" maintxt ,";
					sqlStr+=" releasetime ,";
					sqlStr+=" category ,";
					sqlStr+=" module ,";
					sqlStr+=" releaser ";
		 sqlStr+=" from rule_notice where is_del =0 order by id desc ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountRuleNotice(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select count(1) from rule_notice where is_del = 0 ");
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
				args[i] = "%"+queryValues.get(i)+"%";
			}
		}		
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<RuleNotice> selectLimitRuleNotice(List<String> queryKeys,List<Object> queryValues,int start,int size) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" title ,");
					sqlStr.append(" sketch ,");
					sqlStr.append(" maintxt ,");
					sqlStr.append(" releasetime ,");
					sqlStr.append(" category ,");
					sqlStr.append(" module ,");
					sqlStr.append(" releaser ");
			sqlStr.append(" from rule_notice where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()+2];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
					args[i] = "%"+queryValues.get(i)+"%";
				}
			}
		if(args==null){
			args = new Object[]{start,size};
		}else{
			args[queryKeys.size()] = start;
			args[queryKeys.size()+1] = size;
		}
		sqlStr.append(" order by id desc ");
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<RuleNotice> selectAllRuleNotice(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" title ,");
					sqlStr.append(" sketch ,");
					sqlStr.append(" maintxt ,");
					sqlStr.append(" releasetime ,");
					sqlStr.append(" category ,");
					sqlStr.append(" module ,");
					sqlStr.append(" releaser ");
			sqlStr.append("from rule_notice where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
					args[i] = "%"+queryValues.get(i)+"%";
				}
			}
		sqlStr.append(" order by id desc ");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new RuleNoticeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class RuleNoticeMapper implements RowMapper<RuleNotice> {
		@Override
		public RuleNotice mapRow(ResultSet arg0, int arg1) throws SQLException {
			RuleNotice ruleNotice = new RuleNotice();
			if(arg0!=null){
					ruleNotice.setId(arg0.getInt("id"));
					ruleNotice.setTitle(arg0.getString("title"));
					ruleNotice.setSketch(arg0.getString("sketch"));
					ruleNotice.setMaintxt(arg0.getString("maintxt"));
					ruleNotice.setReleasetime(arg0.getString("releasetime"));
					ruleNotice.setCategory(arg0.getString("category"));
					ruleNotice.setModule(arg0.getString("module"));
					ruleNotice.setReleaser(arg0.getString("releaser"));
				}
			return ruleNotice;
		}  
	}
}

